﻿using System;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.UI.Skins;
using System.Web.UI;
namespace DotNetNuke.Modules.SkinObjectsEx
{
	/// -----------------------------------------------------------------------------
	/// <summary></summary>
	/// <returns></returns>
	/// <remarks></remarks>
	/// <history>
	/// 	[brc]	22/12/2011	new skin object for including js references
    /// </history>
	/// -----------------------------------------------------------------------------

	public class Script : UI.Skins.SkinObjectBase
	{

		// private members
        protected string _scriptType;//jquery/jqueryUI/other
        protected string _path;//Skin/Absolute/Relative
        protected string _location;// location of script 
        protected string _name;//name of script (ie ModuleScript)
        protected string _order;//order.  Valid values are a_BeforejQuery, b_BeforeDnnXml, c_BeforeDomPositioning, d_BeforeDnnControls, e_Default
        protected bool _inHeader;//if true, puts script in header.  If false, puts script in body

		#region public properties

        public string ScriptType
        {
            get
            {
                return _scriptType;
            }
            set
            {
                _scriptType = value;
            }
        }
        public string Path
        {
            get
            {
                return _path;
            }
            set
            {
                _path = value;
            }
        }
        public string Location
        {
            get { return _location; }
            set { _location = value; }
        }
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public string Order
        {
            get { return _order; }
            set { _order = value; }
        }
        public bool InHeader
        {
            get { return _inHeader; }
            set { _inHeader = value;  }
        }
		#endregion

		#region constructors and loads
        public Script()
        {
            Load += Page_Load;
            Init += Page_Init;
        }


		[System.Diagnostics.DebuggerStepThrough()]

		private void InitializeComponent()
		{
		}

		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: This method call is required by the Web Form Designer
			//Do not modify it using the code editor.
			InitializeComponent();
		}

		#endregion

		private void Page_Load(System.Object sender, System.EventArgs e)
		{
            switch (_scriptType.ToLower())
            {
                case "jquery":
                case "jqueryui":
                    bool includeUi = false;
                    if (_scriptType.ToLower() == "jqueryui")
                        includeUi = true;

                    ScriptController.InjectjQueryLibary(this.Page, includeUi, false);
                    break;
                default:
                    string library = "";
                    switch (_path)
                    {
                        case "skin":
                            string skinpath = ((DotNetNuke.UI.Skins.Skin)Parent).SkinPath;
                            library = skinpath + _location;
                            break;
                        case "relative":
                            library = ResolveUrl(_location);
                            break;
                        case "absolute":
                        default:
                            library = _location;
                            break;
                    }
                    if (string.IsNullOrEmpty(library) == false)
                    {
                        //convert the order from the property into the position
                        ScriptController.ScriptInjectOrder position = (ScriptController.ScriptInjectOrder)EnumConversion.ConvertFromEnum(typeof(ScriptController.ScriptInjectOrder), _order, ScriptController.ScriptInjectOrder.e_Default);
                        //inject the script
                        ScriptController.InjectJsLibrary(this.Page, _name, library, _inHeader, position);
                    }
                    break;
            }
		}
	}
}
